<?php
/**
 *  WB_S3 package unit tests
 *
 *  @author Jean-Lou Dupont
 *  @version @@package-version@@
 */

require_once 'PHPUnit/Framework.php';
require_once 'web-bloks/BaseClass/BaseClass.php';
require_once 'web-bloks/ObjFactory/ObjFactory.php'; 
require_once 'web-bloks/AmazonS3/S3.php';
require_once 'web-bloks/AmazonS3/S3Request.php';
require_once 'web-bloks/AmazonS3/responses/S3RawResponse.php';
require_once 'web-bloks/AmazonS3/responses/S3RawResponse_Exception.php';
require_once 'web-bloks/AmazonS3/responses/S3Response.php';
require_once "privateData.php";

class UnitTest extends PHPUnit_Framework_TestCase
{
	var $obj = null;

	/*
	 * GENERIC setup and teardown
	 */		
    protected function setUp()
    {
    	global $AWS_accessKey, $AWS_secretKey;
		
		$this->obj = new WB_S3Request;
		$this->obj->setKeys( $AWS_accessKey, $AWS_secretKey );		
    }
 	protected function tearDown()
	{
		unset( $this->obj );
	}
/*
 * THIS WORKS GREAT BUT SLOWS DOWN TESTS!!! 
 *	
	public function testInvalidHost()
	{
		$this->obj->setHost( "XYZ" );
		$this->obj->setVerb( "GET" );
		$this->obj->setResource( "/" );
		
		$response = $this->obj->send();
		
		$this->assertEquals(false, $response );		
	}
*/	

	public function testValidBucketNames()
	{
		$s3 = WB_ObjFactory::factory( "WB_S3" );
		
		$this->assertEquals( $s3->validateBucketName("192.168.1.1") , false );		
		$this->assertEquals( $s3->validateBucketName("whatever-") , false );				
		$this->assertEquals( $s3->validateBucketName("-whatever") , false );
		$this->assertEquals( $s3->validateBucketName("/whatever") , false );
		$this->assertEquals( $s3->validateBucketName("\whatever") , false );
		$this->assertEquals( $s3->validateBucketName("st") , false );			
		$this->assertEquals( $s3->validateBucketName("-.whatever-.") , false );					
		$this->assertEquals( $s3->validateBucketName("what.-ever") , false );							
		$this->assertEquals( $s3->validateBucketName("#$%^&*()!@") , false );			

		$this->assertEquals( $s3->validateBucketName("jldupont-testcreatebucket") , true );				
	}

    public function testGetBucketList()
    {
    	#$this->obj->setHeaderParam( 'verb', "GET" );
		#$this->obj->setHeaderParam( 'resource', "/" );
		
		$this->obj->setVerb( "GET" );
		$this->obj->setResource( "/" );
		
		$response = $this->obj->send();
		
		$this->assertEquals(true, is_object( $this->obj ) );
		$this->assertEquals(true, is_object( $response ) );		
		
		#var_dump( $response );
    }
	
	public function testCreateBucketThatExists()
	{
		//init keys
    	global $AWS_accessKey, $AWS_secretKey;		
		WB_S3Request::setClassKeys( $AWS_accessKey, $AWS_secretKey );
		
		$s3 = WB_ObjFactory::factory( "WB_S3" );
		
		$exp = null;
		try
		{
			$response = $s3->createBucket( 'jldupont-test' );
		}
		catch( Exception $e )
		{
			$exp = $e;
		}
		#$this->assertEquals("WB_S3RawResponse_Exception", get_class( $exp ) );
		#$this->assertEquals( $response, false );
		#var_dump( $response );
	}

	/**
	 * TestInvalidKeys
	 * 
	 * Do not initialize the keys and
	 * let an exception out.
	 * 
	 */
	public function testInvalidKeys()
	{
    	#global $AWS_accessKey, $AWS_secretKey;		
		WB_S3Request::setClassKeys( $AWS_accessKey, $AWS_secretKey );
		
		$s3 = WB_ObjFactory::factory( "WB_S3" );

		$exp = null;
		try {		
		$response = $s3->getBucketList();
		}
		catch( Exception $e )
		{
			$exp = $e;
		}
		$this->assertEquals("WB_S3RawResponse_Exception", get_class( $exp ) );						
	}

	public function testGetBucketList2()
	{
    	global $AWS_accessKey, $AWS_secretKey;		
		WB_S3Request::setClassKeys( $AWS_accessKey, $AWS_secretKey );
		
		$s3 = WB_ObjFactory::factory( "WB_S3" );
		
		$response = $s3->getBucketList();
		
		$this->assertEquals(true, is_object( $s3 ) );
		$this->assertEquals(true, is_object( $response ) );		
		$this->assertEquals("WB_S3Response_BucketList", get_class( $response ) );				
		
		foreach( $response as $name => $objMeta )
		{
			$this->assertEquals(true, is_string( $name ) );			
			$this->assertEquals("WB_S3BucketInfo", get_class( $objMeta ) );
		}
	}
	public function testGetObjectList1()
	{
    	global $AWS_accessKey, $AWS_secretKey;		
		WB_S3Request::setClassKeys( $AWS_accessKey, $AWS_secretKey );
		
		$s3 = WB_ObjFactory::factory( "WB_S3" );
		
		$response = $s3->getObjectList( 'jldupont-test' );

		$this->assertEquals(true, is_object( $s3 ) );
		$this->assertEquals(true, is_object( $response ) );		
		$this->assertEquals("WB_S3Response_ObjectList", get_class( $response ) );				

		foreach( $response as $index => $objMeta )
		{
			$this->assertEquals( true, is_numeric( $index ) );			
			$this->assertEquals('WB_S3ObjectInfo', get_class( $objMeta ) );						
		}
	}

	public function testDeleteNonExistingBucket()
	{
    	global $AWS_accessKey, $AWS_secretKey;		
		WB_S3Request::setClassKeys( $AWS_accessKey, $AWS_secretKey );
		
		$s3 = WB_ObjFactory::factory( "WB_S3" );
		
		$response = $s3->deleteBucket( 'jldupont-notbucket' );
		
		$this->assertEquals("WB_S3RawResponse", get_class( $response ) );				
		$this->assertEquals(404, $s3->getLastCode() );		
	}

	public function testCreateBucket()
	{
		//init keys
    	global $AWS_accessKey, $AWS_secretKey;		
		WB_S3Request::setClassKeys( $AWS_accessKey, $AWS_secretKey );
		
		$s3 = WB_ObjFactory::factory( "WB_S3" );
		
		$response = $s3->createBucket( 'jldupont-testcreatebucket' );
		
		$this->assertEquals(true, is_bool( $response ) );		
		$this->assertEquals(true, $response );		
	}


	public function testPutObject1()
	{
		$file = file_get_contents( __FILE__ );
		
    	global $AWS_accessKey, $AWS_secretKey;		
		WB_S3Request::setClassKeys( $AWS_accessKey, $AWS_secretKey );
		
		$s3 = WB_ObjFactory::factory( "WB_S3" );
		
		$response = $s3->putObject(	'jldupont-testcreatebucket', 
									'test.php', 
									$file, 
									array( 'Content-Type' => 'application/octet-stream') );
		
		$this->assertEquals(true, $response );
	}

	public function testGetObject1()
	{
		$file = file_get_contents( __FILE__ );
		
    	global $AWS_accessKey, $AWS_secretKey;		
		WB_S3Request::setClassKeys( $AWS_accessKey, $AWS_secretKey );
		
		$s3 = WB_ObjFactory::factory( "WB_S3" );
		
		$response = $s3->getObject( 'jldupont-testcreatebucket', 'test.php' );
		
		$this->assertEquals( $file, $response );
	}

	public function testDeleteObject()
	{
		//init keys
    	global $AWS_accessKey, $AWS_secretKey;		
		WB_S3Request::setClassKeys( $AWS_accessKey, $AWS_secretKey );
		
		$s3 = WB_ObjFactory::factory( "WB_S3" );
		
		$response = $s3->deleteObject( 'jldupont-testcreatebucket', 'test.php' );
		
		$this->assertEquals(true, is_bool( $response ) );		
		$this->assertEquals(true, $response );		
	}

	public function testDeleteCreateBucket()
	{
		//init keys
    	global $AWS_accessKey, $AWS_secretKey;		
		WB_S3Request::setClassKeys( $AWS_accessKey, $AWS_secretKey );
		
		$s3 = WB_ObjFactory::factory( "WB_S3" );
		
		$response = $s3->deleteBucket( 'jldupont-testcreatebucket' );
		
		$this->assertEquals(true, is_bool( $response ) );		
		$this->assertEquals(true, $response );		
	}

}// end UnitTest class